import 'dart:io';

import 'package:image_picker/image_picker.dart';
import 'package:permission_handler/permission_handler.dart';

/// 相机
final int PICKER_CAMERA = 001;

/// 相册
final int PICKER_PHOTO = 002;

class ImagePickerUtils {
  static Future<File> getImage(int pickerType) async {
    if (await requestPhotoPermission()) {
      final pickedFile = await ImagePicker().getImage(
          source: pickerType == PICKER_CAMERA
              ? ImageSource.camera
              : ImageSource.gallery);

      if (pickedFile != null) {
        return File(pickedFile.path);
      } else {
        return null;
        print('No image selected.');
      }
    }
    return null;
  }

  /// 请求相册/相机权限
  static Future<bool> requestPhotoPermission() async {
    //获取当前的权限
    Map<Permission, PermissionStatus> statuses = await [
      Permission.photos,
    ].request();
    if (statuses[Permission.photos].isGranted) {
      print("同意了");
      return true;
    } else if (statuses[Permission.photos].isDenied) {
      print("拒绝了");
      return false;
    } else if (statuses[Permission.photos].isPermanentlyDenied) {
      print("永久拒绝");
      return false;
    }
  }
}
